<?php
/*******************************************************************************
 * $Date:: Jan 7, 2011 3:45:13 PM                                              $
 * $Revision:                                                                  $
 * $Author:: Tuan Duong                                                        $
 * $Comment::                                                                  $
 ******************************************************************************/ 
/**
 * Description here...
 * 
 * @package Lua
 * @subpackage Application
 * @author Tuan Duong <tuan[at]smartdzine[dot]com>
 * @version 
 */

class Lua_Application
{
    /**
     * @var Lua_Dispatcher_Interface
     */
    private $dispatcher;

    /**
     * @var Lua_Router_Abstract
     */
    private $router;

    private $configs = array();
    
    public function __construct($configs = null)
    {
        require_once 'Zend/Loader/Autoloader.php';
        $autoLoader = Zend_Loader_Autoloader::getInstance();
        $autoLoader->setFallbackAutoloader(true);
        
        if (null == $configs) {
            // Load configuration
            Lua_Config::load(array('application', 'database'));
            $configs = Lua_Config::getConfig();
        }

        $this->setConfigs($configs);

        $this->initialize();
    }

    private function initialize()
    {
        Lua_Registry::set(Lua_Constant_General::LUA_CONFIG, $this->configs);

        $routerClass = Lua_Constant_General::DEFAULT_ROUTER;
        $dispatcherClass = Lua_Constant_General::DEFAULT_DISPATCHER;
        $viewAdapter = Lua_Constant_General::DEFAULT_VIEW_ADAPTER;
        if (isset($this->configs['lua']['default'])) {
            $defaultConfigs = $this->configs['lua']['default'];
            if (isset($defaultConfigs['router'])) {
                $routerClass = $defaultConfigs['router'];
            }

            if (isset($defaultConfigs['dispatcher'])) {
                $routerClass = $defaultConfigs['router'];
            }
        }
        $this->setDispatcher(new $dispatcherClass());
        $this->setRouter(new $routerClass());


        // Register router and dispatcher
        Lua_Registry::set(Lua_Constant_General::LUA_ROUTER, $this->router);
        Lua_Registry::set(Lua_Constant_General::LUA_DISPATCHER, $this->dispatcher);

        // Database adapter intialize
        if (isset($this->configs['resources']['db'])) {
            $dbConfigs = $this->configs['resources']['db'];
            $db = Zend_Db::factory($dbConfigs['adapter'], $dbConfigs['params']);
            Zend_Db_Table_Abstract::setDefaultAdapter($db);
        }

    }
    
    /**
     * @return Lua_Dispatcher_Interface
     */
    public function getDispatcher()
    {
        return $this->dispatcher;
    }

    /**
     * @return Lua_Router_Abstract
     */
    public function getRouter() {
        return $this->router;    
    }

    public function run()
    {
        $this->dispatcher->dispatch($this->router);
    }

    /**
     * @param  $configs
     * @return void
     */
    public function setConfigs(array $configs)
    {
        $this->configs = $configs;
    }

    /**
     * @param Lua_Router_Abstract $router
     * @return void
     */
    public function setRouter(Lua_Router_Abstract $router)
    {
        $this->router = $router;
    }

    /**
     * @param Lua_Dispatcher_Interface $dispatcher
     * @return void
     */
    public function setDispatcher(Lua_Dispatcher_Interface $dispatcher)
    {
        $this->dispatcher = $dispatcher;        
    }
}